const $login = require("@src/components/login/index.js");
export const _login = (force) => {
  console.info("全局发起微信登录");
  wx.login({
    success: (res) => {
      if (res.code) {
        $login
          .loginApi({
            code: res.code,
            webviewAuth: force,
          })
          .then((resp) => {
            res = resp.data;
            console.log("reslogin", res);
            if (res.code === 0) {
              $login.setToken(res.data.token, res.data.expireTime);
              $login.setAnon(res.data.anonUser, res.data.expireTime);
            } else if (res.code === 61000) {
              wx.navigateTo({
                url:
                  $login.webviewPath +
                  `?loginParams=${JSON.stringify({
                    webViewUrl: encodeURIComponent(res.data.url),
                  })}`,
              });
            } else {
              wx.showToast({
                title: res.msg,
                icon: "none",
                duration: 5000,
              });
            }
          })
          .catch((error) => {
            console.error(error);
            wx.showToast({
              title: error,
              icon: "none",
              duration: 5000,
            });
          });
      } else {
        wx.showToast({
          title: "网络异常",
          icon: "none",
          duration: 5000,
        });
      }
    },
  });
};
